{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Patron\n",
    "## An example of the Digital Twin\n",
    "\n",
    "This example just includes:\n",
    "* Connecting\n",
    "* Uploading models\n",
    "* Creating twins\n",
    "* Querying twins\n",
    "\n",
    "It's not a lot, but it will all tie together when we start running scenarios. \n",
    "\n",
    "[This is the SDK repo on Github](https://github.com/Azure/azure-sdk-for-python/tree/4559e19e2f3146a49f1eba1706bb798071f4a1f5/sdk/digitaltwins/azure-digitaltwins-core)\n",
    "\n",
    "[Here is the doc on the query language](https://docs.microsoft.com/en-us/azure/digital-twins/concepts-query-language)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connecting\n",
    "\n",
    "**note**: you will need to replace {`home-test-twin.api.wcus.digitaltwins.azure.net`} with the address of your ADT. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<azure.digitaltwins.core._digitaltwins_client.DigitalTwinsClient at 0x23e31827e48>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from azure.identity import AzureCliCredential\n",
    "from azure.digitaltwins.core import DigitalTwinsClient\n",
    "\n",
    "# using yaml instead of \n",
    "import yaml\n",
    "import uuid\n",
    "\n",
    "# using altair instead of matplotlib for vizuals\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# you will get this from the ADT resource at portal.azure.com\n",
    "your_digital_twin_url = \"home-test-twin.api.wcus.digitaltwins.azure.net\"\n",
    "\n",
    "azure_cli = AzureCliCredential()\n",
    "service_client = DigitalTwinsClient(\n",
    "    your_digital_twin_url, azure_cli)\n",
    "service_client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<azure.digitaltwins.core._digitaltwins_client.DigitalTwinsClient at 0x23e31827e48>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "service_client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Loading the model from a local Json file, I'm defining a model for a `Patron`. That's like a `class` that will be used to make several instances of a customer."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Uploading models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "patron_model_id = \"dtmi:mymodels:patron;1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Delete the model that you don't want. \n",
    "# service_client.delete_model(patron_model_id)\n",
    "\n",
    "# # Create it if you just deleted it.\n",
    "# patron_model_json = yaml.safe_load(open(\"models/patron.json\"))\n",
    "# service_client.create_models([patron_model_json])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the json must be in a list. This is so that you can deploy several models in a single go."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'display_name': {'en': 'Patron'},\n",
       " 'description': {'en': 'As an example, contains all of the properties possible in the DTDL.'},\n",
       " 'id': 'dtmi:billmanh:patron;1',\n",
       " 'upload_time': '2020-11-19T02:14:27.21094Z',\n",
       " 'decommissioned': False}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the Patron model\n",
    "get_model = service_client.get_model(patron_model_id)\n",
    "get_model.as_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In my case I use `customer` as an _instance_ of a model (a _twin_). The only thing to add is the information specific to the twin I want to upload. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating twins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "digital_twin_id = 'customer-' + str(uuid.uuid4())\n",
    "\n",
    "customer_json = {\n",
    "    \"$metadata\": {\n",
    "        \"$model\": patron_model_id\n",
    "    },\n",
    "    \"satisfaction\": 10,\n",
    "    \"totalWaitTime\": 10\n",
    "}\n",
    "\n",
    "created_twin = service_client.upsert_digital_twin(digital_twin_id, customer_json)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can see that the process with the API is simple:\n",
    "* Define a model\n",
    "* Build a twin from that model\n",
    "* Give that model some attributes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(created_twin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'$dtId': 'customer-3cbd5e60-957d-44ff-944f-9adb42a20a52',\n",
       " '$etag': 'W/\"ea33ae65-f47c-4116-8e3d-1c6fa61777d6\"',\n",
       " 'satisfaction': 10,\n",
       " 'totalWaitTime': 10,\n",
       " '$metadata': {'$model': 'dtmi:billmanh:patron;1',\n",
       "  'satisfaction': {'lastUpdateTime': '2020-11-19T15:48:13.1057699Z'},\n",
       "  'totalWaitTime': {'lastUpdateTime': '2020-11-19T15:48:13.1057699Z'}}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "created_twin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So let's create a bunch of people. Let's pretend we have a single room full of a bunch of people that all have similar attributes:\n",
    "* `satisfaction` varies from person to person.\n",
    "* `totalWaitTime` is the same at the beginning. \n",
    "\n",
    "the item for the `$metadata` is the model that it will be an instance of. The rest of the items in the dict are the `contents` of the model as you defined it.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('customer-5c454e2f-f70b-4352-b75a-958f1a49beba',\n",
       "  {'$metadata': {'$model': 'dtmi:billmanh:patron;1'},\n",
       "   'satisfaction': 7,\n",
       "   'totalWaitTime': 0}),\n",
       " ('customer-26196fee-5ffd-457a-86b7-192a998f3cf2',\n",
       "  {'$metadata': {'$model': 'dtmi:billmanh:patron;1'},\n",
       "   'satisfaction': 9,\n",
       "   'totalWaitTime': 0}),\n",
       " ('customer-e6f49d8a-711b-41c3-9db8-c7ece3dbc32c',\n",
       "  {'$metadata': {'$model': 'dtmi:billmanh:patron;1'},\n",
       "   'satisfaction': 7,\n",
       "   'totalWaitTime': 0}),\n",
       " ('customer-c87adbfa-1c6e-4ea9-9f03-83e3877ef5fc',\n",
       "  {'$metadata': {'$model': 'dtmi:billmanh:patron;1'},\n",
       "   'satisfaction': 8,\n",
       "   'totalWaitTime': 0}),\n",
       " ('customer-21e17d28-76c3-4c04-8df9-396703692a68',\n",
       "  {'$metadata': {'$model': 'dtmi:billmanh:patron;1'},\n",
       "   'satisfaction': 8,\n",
       "   'totalWaitTime': 0})]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def create_new_customer():\n",
    "    digital_twin_id = 'customer-' + str(uuid.uuid4())\n",
    "    customer_json = {\n",
    "        \"$metadata\": {\n",
    "            \"$model\": patron_model_id\n",
    "        },\n",
    "        \"satisfaction\": np.random.randint(5,10),\n",
    "        \"totalWaitTime\": 0\n",
    "    }\n",
    "    return digital_twin_id,customer_json\n",
    "    \n",
    "customer_twin_examples = [create_new_customer() for i in range(40)]\n",
    "customer_twin_examples[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div id=\"altair-viz-f081d4d3577149bb88f3ec23383f1645\"></div>\n",
       "<script type=\"text/javascript\">\n",
       "  (function(spec, embedOpt){\n",
       "    let outputDiv = document.currentScript.previousElementSibling;\n",
       "    if (outputDiv.id !== \"altair-viz-f081d4d3577149bb88f3ec23383f1645\") {\n",
       "      outputDiv = document.getElementById(\"altair-viz-f081d4d3577149bb88f3ec23383f1645\");\n",
       "    }\n",
       "    const paths = {\n",
       "      \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
       "      \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
       "      \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.8.1?noext\",\n",
       "      \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
       "    };\n",
       "\n",
       "    function loadScript(lib) {\n",
       "      return new Promise(function(resolve, reject) {\n",
       "        var s = document.createElement('script');\n",
       "        s.src = paths[lib];\n",
       "        s.async = true;\n",
       "        s.onload = () => resolve(paths[lib]);\n",
       "        s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
       "        document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "      });\n",
       "    }\n",
       "\n",
       "    function showError(err) {\n",
       "      outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
       "      throw err;\n",
       "    }\n",
       "\n",
       "    function displayChart(vegaEmbed) {\n",
       "      vegaEmbed(outputDiv, spec, embedOpt)\n",
       "        .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
       "    }\n",
       "\n",
       "    if(typeof define === \"function\" && define.amd) {\n",
       "      requirejs.config({paths});\n",
       "      require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
       "    } else if (typeof vegaEmbed === \"function\") {\n",
       "      displayChart(vegaEmbed);\n",
       "    } else {\n",
       "      loadScript(\"vega\")\n",
       "        .then(() => loadScript(\"vega-lite\"))\n",
       "        .then(() => loadScript(\"vega-embed\"))\n",
       "        .catch(showError)\n",
       "        .then(() => displayChart(vegaEmbed));\n",
       "    }\n",
       "  })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-5550894ec7db2b3ea851a9058ab41163\"}, \"mark\": \"bar\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"bin\": true, \"field\": \"satisfaction\"}, \"y\": {\"type\": \"quantitative\", \"aggregate\": \"count\", \"title\": \"# customers\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.8.1.json\", \"datasets\": {\"data-5550894ec7db2b3ea851a9058ab41163\": [{\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 6, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 6, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 6, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 6, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 9, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 5, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 8, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 6, \"totalWaitTime\": 0}, {\"$metadata\": {\"$model\": \"dtmi:billmanh:patron;1\"}, \"satisfaction\": 7, \"totalWaitTime\": 0}]}}, {\"mode\": \"vega-lite\"});\n",
       "</script>"
      ],
      "text/plain": [
       "alt.Chart(...)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer_df = pd.DataFrame([i[1] for i in customer_twin_examples])\n",
    "\n",
    "customer_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course, those are just local customer data points. You could imagine that this is information collected at the edge. \n",
    "\n",
    "Now let's upload those people to see them in the cloud. Try to imagine that each customer in the store had an app on their phone that sent this information to the graph. Each `customer` represents a data feed from IoT telemetry. Generally, each device would upload a separate feed of IoT, but we are simulating using a single device (your computer) to upload this data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in customer_twin_examples:\n",
    "    service_client.upsert_digital_twin(i[0], i[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's query our customers to see how they are feeling. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying twins\n",
    "This is pretty easy. If you've ever done SQL you'll get this right away.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_expression = 'SELECT * FROM digitaltwins'\n",
    "query_result = service_client.query_twins(query_expression)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in query_result:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'$dtId': 'customer-2e551c55-265a-46e0-a84b-4965bc734e21',\n",
       " '$etag': 'W/\"8f1be988-70af-4488-a7c9-5403a4b6ea1d\"',\n",
       " 'satisfaction': 9,\n",
       " 'totalWaitTime': 0,\n",
       " '$metadata': {'$model': 'dtmi:billmanh:patron;1',\n",
       "  'satisfaction': {'lastUpdateTime': '2020-11-19T16:13:44.9882083Z'},\n",
       "  'totalWaitTime': {'lastUpdateTime': '2020-11-19T16:13:44.9882083Z'}}}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's say I want to get all of the customers that have > 7 satisfaction _Happy customers_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = service_client.query_twins(\n",
    "\"\"\"\n",
    "SELECT  *\n",
    "FROM DigitalTwins T  \n",
    "WHERE T.satisfaction > 7\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "customer-cc04f3b6-39b0-4cef-bfff-a7d668cce446 10\n",
      "customer-3cbd5e60-957d-44ff-944f-9adb42a20a52 10\n",
      "customer-26196fee-5ffd-457a-86b7-192a998f3cf2 9\n",
      "customer-c87adbfa-1c6e-4ea9-9f03-83e3877ef5fc 8\n",
      "customer-21e17d28-76c3-4c04-8df9-396703692a68 8\n",
      "customer-8375cece-abf8-400f-be66-459f7f40fe6a 8\n",
      "customer-d0483de8-0f9f-41d4-ac0c-84d608f7467a 8\n",
      "customer-8ae03d4b-ee2c-4ffd-9aa8-8edbfbf39728 9\n",
      "customer-fc568319-d5a9-492a-83ce-fed774257003 8\n",
      "customer-d9ca8715-1a6d-43ea-83d2-671fc972ebd7 9\n",
      "customer-acd7a91f-1d28-46b3-894d-6f3953fbd85e 8\n",
      "customer-a867aacb-68a7-4872-a3bb-330b0b34ef7c 9\n",
      "customer-373017d3-d7e2-4407-8c5b-5fc44e20286f 9\n",
      "customer-418cbc74-b101-4f50-8cf0-6075a2a8053c 8\n",
      "customer-04cc7f9d-5503-4d64-8dbc-373a47a8710a 9\n",
      "customer-e5d090d6-ae68-4c31-890e-16ce96c1e462 9\n",
      "customer-12a558cf-b15d-41ee-a98b-d07c4b7411ee 8\n",
      "customer-1c51c2a0-45bc-442c-b4df-1d193e11f628 8\n",
      "customer-ca674938-4d13-463f-8858-e0b9ac5deebc 9\n",
      "customer-2139a9c9-ff79-4a63-9576-c34744f0521d 9\n",
      "customer-2e551c55-265a-46e0-a84b-4965bc734e21 9\n",
      "customer-107c1168-d86b-40e1-a57d-4c500148e172 10\n",
      "customer-73299891-e2f2-4d29-98f1-9d2ee0e9f12b 9\n",
      "customer-d1de64aa-cfc2-4385-9644-667506f0f474 9\n",
      "customer-d4e84d9f-ee9d-45e8-ba9f-d9d90885f578 9\n",
      "customer-b7a9fbba-8e06-4b23-ac39-35acfbacfe7c 8\n",
      "customer-83f65979-cd94-41da-b248-c6a1a76e2f13 9\n",
      "customer-84e9f81b-db4c-40c7-8284-230a357937a7 8\n",
      "customer-06c5c57e-aec0-47c4-ac78-b986e589da15 8\n",
      "customer-26a50817-189d-48cf-8ecc-efa9828f092a 9\n",
      "customer-45e9aa03-733d-4a99-b9d5-94f1c6b04214 9\n",
      "customer-0234cb48-1fa2-43e0-b69d-36a6ff269666 9\n",
      "customer-048f85a8-173e-4305-92b8-ead2a748b07f 8\n",
      "customer-25e19268-3433-4f09-afe3-94f466313368 10\n"
     ]
    }
   ],
   "source": [
    "for i in query_result:\n",
    "    print (i['$dtId'],i['satisfaction'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course, you can just pull everything, but this might be problematic as you build a larger model. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = service_client.query_twins(\n",
    "\"\"\"\n",
    "SELECT  COUNT() \n",
    "FROM DigitalTwins \n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'COUNT': 88}\n"
     ]
    }
   ],
   "source": [
    "for i in query_result:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (azure_test)",
   "language": "python",
   "name": "azure_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}